#!/usr/local/bin/python3
# -*- coding: utf-8 -*-

"""
@File    : test.py
@Author  : lepal
@Time    : 2024-02-29 17:16
"""
# from flask import Blueprint
import requests
from flask import request
from app.libs.RedPrint import RedPrint

# user = Blueprint('user', '__main__')
api = RedPrint('test')


# 获取ip地址
# @app.route('/'), 因为app核心对象在入口文件中，如果把入口文件的app对象在当前导入会导致循环导入，所以使用蓝图解决
@api.route('/get_public_ip')
def index():
    forwarded_ips = request.headers.getlist("X-Forwarded-For")
    print(request.environ)		# 获取外网IP

    if forwarded_ips:
        client_ip = forwarded_ips[0]
    else:
        # 如果没有经过代理，则REMOTE_ADDR是客户端公网IP
        client_ip = request.remote_addr

    # 清理IP地址，防止头部被恶意篡改或包含额外信息
    client_ip = client_ip.strip()

    return f"客户端IP地址是_>：{client_ip}"


# 根据第三方库 获取公网ip
@api.route('/get_public_ip2')
def get_external_ip():
    try:
        ip = requests.get('https://ident.me').text.strip()
        return ip
    except:
        return None
